package it.unibz.ti2.internetPizzeria.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.Order;

import it.unibz.ti2.internetPizzeria.entities.Property;

/**
 * Class reponsible for fetching single properties and lists of properties form database and modifying properties.
 */
public class PropertyDAO extends BaseDAO {

	/**
	 * Retrieves property of specified key from database
	 * @param key property's key
	 * @return property of specified key
	 */
	public Property getProperty(String key){
		Criteria criteria = session.createCriteria(Property.class);
		criteria.add(Expression.eq("key", key));
		Property result = (Property)criteria.uniqueResult();
		return result;
	}

	/**
	 * Retrieves properties of specified editable value from database. Returned properties are sorted ascending by property key.
	 * @param editable defines if property should be editable or not
	 * @return list of properties of specified editable value
	 */
	public List<Property> getAllProperties(boolean editable) {
		Criteria criteria = session.createCriteria(Property.class);
		criteria.addOrder(Order.asc("key"));
		criteria.add(Expression.eq("allowEdit", editable));
		List<Property> result = criteria.list();
		return result;
	}
	
	/**
	 * Retrieves all properties from database. Returned properties are sorted ascending by property key.
	 * @return list of all properties 
	 */
	public List<Property> getAllProperties() {
		Criteria criteria = session.createCriteria(Property.class);
		criteria.addOrder(Order.asc("key"));
		List<Property> result = criteria.list();
		return result;
	}

	/**
	 * Updates property in database
	 * @param property to be updated
	 */
	public void update(Property property) {
		super.updateObject(property);
	}


}
